<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" clearable @change="handleFilter" class="filter-item" style="width: 200px;" placeholder="请输入原料名称" />
      <el-date-picker
        clearable
        class="filter-item"
        v-model="time"
        @change="changeTime"
        type="datetimerange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期">
      </el-date-picker>
      <el-button v-permission="['GET /admin/formula/report']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border show-summary fit highlight-current-row>
      <el-table-column align="center" label="原料ID" prop="id" sortable />

      <el-table-column align="center" label="原料名称" prop="name" />

      <el-table-column align="center" label="原料单价" prop="price" />

      <el-table-column align="center" label="用量" prop="amount" />

      <el-table-column align="center" label="原料单位" prop="unit" />


      <el-table-column align="center" label="成本(元)" prop="totalPrice" />

    </el-table>
    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>
  </div>
</template>

<style>
  .table-expand {
    font-size: 0;
  }
  .table-expand label {
    width: 100px;
    color: #99a9bf;
  }
  .table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
  }
  .goods-detail-box img {
    width: 100%;
  }
</style>

<script>
import { materailReport } from '@/api/material'
import { getToken } from '@/utils/auth'
import BackToTop from '@/components/BackToTop'

export default {
  name: 'MaterailReport',
  components: { BackToTop },
  data() {
    return {
      time: [],
      list: [],
      downloadLoading: false,
      listQuery: {
        name: undefined,
        startTime: undefined,
        endTime: undefined
      },
    }
  },
  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  },
  created() {
    // this.initTime()
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      materailReport(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.listLoading = false
        })
    },
    handleFilter() {
      this.getList()
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['原料ID', '原料名称', '用量', '原料单位', '成本']
        const filterVal = ['id', 'name', 'amount', 'unit', 'price']
        excel.export_json_to_excel2(tHeader, this.list, filterVal, '配方报表')
        this.downloadLoading = false
      })
    },
    initTime() {
      const end = new Date(new Date(new Date().toLocaleDateString()).getTime());
      const start = new Date(new Date(new Date().toLocaleDateString()).getTime()-3600 * 1000 *24);
      this.listQuery.endTime=end.getTime()
      this.listQuery.startTime=start.getTime()
      this.time=[start,end]
    },
    changeTime(time) {
      this.listQuery.startTime=time[0].getTime()
      this.listQuery.endTime=time[1].getTime()
      this.getList()
    }
  }
}
</script>
